package com.xiaoyu.lanling.feature.guard.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.xiaoyu.base.app.GlobalUI
import com.xiaoyu.base.event.AppEventBus
import com.xiaoyu.base.event.SimpleEventHandler
import com.xiaoyu.base.model.User
import com.xiaoyu.base.utils.extensions.setOnClickDebounceListener
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.activity.base.BaseDialogFragment
import com.xiaoyu.lanling.common.Constant
import com.xiaoyu.lanling.event.gift.GiveGiftAndSendMessageEvent
import com.xiaoyu.lanling.event.guard.ClickGiveUpGuardEvent
import com.xiaoyu.lanling.event.guard.CloseEvent
import com.xiaoyu.lanling.event.guard.GiveUpGuardEvent
import com.xiaoyu.lanling.feature.guard.data.GuardData
import com.xiaoyu.lanling.media.image.ImageViewUtil
import kotlinx.android.synthetic.main.guard_detail_dialog_fragment.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class GuardDetailDialogFragment : BaseDialogFragment() {

    private val requestTag = Any()

    companion object {
        val TAG = GuardDetailDialogFragment::class.java.simpleName

        fun show(fragmentManager: FragmentManager, guardee: User, guarder: User, coinNum: Int, from: String) {
            if (guardee.isSelf && guarder.isNobody) {
                GlobalUI.getInstance().showToast(R.string.guard_detail_me_nobody_toast)
                return
            }

            val arguments = Bundle()
            arguments.putSerializable(Constant.IntentKey.KEY_GUAREE, guardee)
            arguments.putSerializable(Constant.IntentKey.KEY_GUARER, guarder)
            arguments.putInt(Constant.IntentKey.KEY_COIN_NUM, coinNum)
            arguments.putString(Constant.PARAM_FROM, from)

            val fragment = GuardDetailDialogFragment()
            fragment.arguments = arguments
            fragment.show(fragmentManager, TAG)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogFullScreenDimStyle)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.guard_detail_dialog_fragment, container, false)
    }

    override fun onViewCreatedSafelyAfterAppFinishInit(view: View, savedInstanceState: Bundle?) {
        initView()
        initEvent()
        initBind()
    }

    private fun initView() {

        val guardee = (arguments?.getSerializable(Constant.IntentKey.KEY_GUAREE) as? User) ?: return
        val guarder = (arguments?.getSerializable(Constant.IntentKey.KEY_GUARER) as? User) ?: return
        val coinNum = arguments?.getInt(Constant.IntentKey.KEY_COIN_NUM) ?: return
        val from = arguments?.getString(Constant.PARAM_FROM) ?: ""

        ImageViewUtil.loadAvatarRoundWithBoard(guardee_avatar, guardee, 72, 2, R.color.user_info_avatar_border, false)
        ImageViewUtil.loadAvatarRoundWithBoard(guarder_avatar, guarder, 40, 2, R.color.guard_avatar_boarder, false)

        val fragment = when {
            guardee.isSelf -> GuardDetailMeGuardedFragment.newInstance(guardee, guarder, from)
            guarder.isSelf -> GuardDetailOtherGuardingFragment.newInstance(guardee, guarder, from)
            guarder.isNobody -> GuardDetailOtherNobodyFragment.newInstance(guardee, guarder, from, coinNum)
            else -> GuardDetailOtherGuardedFragment.newInstance(guardee, guarder, from, coinNum)

        }
        childFragmentManager.beginTransaction()
                .replace(R.id.guard_detail_bottom_container, fragment)
                .commit()
    }

    private fun initEvent() {
        AppEventBus.bindContainerAndHandler(this, object : SimpleEventHandler() {
            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: ClickGiveUpGuardEvent) {
                GuardData.giveUpGuard(requestTag, event.uid)
            }

            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: CloseEvent) {
                dismiss()
            }

            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: GiveUpGuardEvent) {
                if (event.isNotFromThisRequestTag(requestTag)) {
                    return
                }
                if (!isAdded || isDetached || !event.result) {
                    return
                }
                dismiss()
            }

            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: GiveGiftAndSendMessageEvent) {
                val from = arguments?.getString(Constant.PARAM_FROM) ?: ""
                if (from != event.from) {
                    return
                }
                if (!isAdded || isDetached) {
                    return
                }
                dismiss()
            }
        })
    }

    private fun initBind() {
        close?.setOnClickDebounceListener { dismiss() }
    }
}